<template>
	<div>
		<smpPanel>
			<el-form :inline="false" label-width="100px" size="mini" ref="form" :model="query.body">
				<el-row>
					<el-col :span="6">
						<el-form-item label="用户名:" prop="userName">
							<el-input v-model="query.body.userName"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="用户中文名:" prop="userChineseName">
							<el-input v-model="query.body.userChineseName"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
							<searchButtonGroup @search="getList" @reset="reset" online  />
					</el-col>
				</el-row>
				</el-row>
			</el-form>
		</smpPanel>
		<smpPanel title="用户列表">
			<createButton slot="option"  @click="addUser"  />
			<el-table :data="list" size="mini" stripe border style="width: 100%">
				<el-table-column prop="userName" label="用户名" width="300" align="center">
				</el-table-column>

				<el-table-column prop="userChineseName" label="用户中文名" align="center">
				</el-table-column>

				<el-table-column prop="phone" label="联系电话" align="center">
				</el-table-column>

				<el-table-column prop="status" label="状态" align="center">
					<template slot-scope="scope">
						{{scope.row.status|dictName("SMP0005")}}
					</template>
				</el-table-column>

				<el-table-column prop="pinyin" label="用户拼音名" align="center">
				</el-table-column>

				<!-- <el-table-column prop="department" label="部门" align="center"></el-table-column> -->
				<el-table-column prop="orgName" label="部门" align="center"></el-table-column>

				<el-table-column prop="roleNames" label="角色" align="center" show-overflow-tooltip>
				</el-table-column>

				<el-table-column label="操作" align="center" width="150">
					<template slot-scope="scope">


						<el-link size="mini" type="primary" :underline="false" style="font-size: 12px;"
							@click="handleEdit(scope.$index,scope.row)">修改</el-link>

						<el-divider direction="vertical"></el-divider>

						<el-link size="mini" type="primary" :underline="false" style="font-size: 12px;"
							@click="resetPasswd(scope.row)">重置密码</el-link>
						<!-- | 
							 <el-link
							   size="mini
							 						 type="primary"
							 						 :underline="false"
							 						 style="font-size: 12px;"
							   @click="handRolePreserve(scope.$index, scope.row)">角色维护</el-link> -->

					</template>
				</el-table-column>

			</el-table>
			<div class="smp-pagination" v-show="total>0">
				<el-pagination background size="medium" layout="total,sizes,prev, pager, next,jumper"
					:page-size="query.pageSize" :page-sizes="[5,10,15,20]" :total="total" :current-page="query.pageNum"
					@current-change="currentChange" @size-change="sizeChange">
				</el-pagination>
			</div>
		</smpPanel>
		<user ref="user" @fresh="fresh" />
		<userEdit ref="userEdit" @fresh="fresh" />
		<rolePreserve ref="rolePreserve" @fresh="fresh" />

	</div>
</template>

<script>
	"use strict";
	import {
		selectByPage,
		resetPasswd
	} from '@/api/user.js';
	import user from "./user";
	import userEdit from "./userEdit";
	import rolePreserve from "./rolePreserve";
	export default {
		data() {
			return {
				list: [],
				query: {
					body: {
						userName: "",
						userChineseName: ""
					},
					pageNum: 1,
					pageSize: 5
				},
				total: 0
			};



		},
		created() {
			Object.assign(this.query.body, this.$route.query);
			if (this.$route.query.pageNum != undefined) {
				this.query.pageNum = this.$route.query.pageNum;
			}
			if (this.$route.query.pageSize != undefined) {
				this.query.pageSize = parseInt(this.$route.query.pageSize);
			}
			this.getList();
		},
		components: {
			user,
			userEdit,
			rolePreserve
		},
		methods: {


			resetPasswd(row) {
				this.$confirm('确认重置密码?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					resetPasswd(row).then(response => {
						let type = response.data.code == '0000' ? 'success' : 'error'
						this.$message({
							duration: 5000,
							type: type,
							message: response.data.message
						});
					});

				}).catch(() => {
					this.$message({
						duration: 5000,
						type: 'info',
						message: '已取消删除'
					});
				});

			},

			addUser() {
				this.$refs.user.ShowDialog();
			},

			/**
			 * @param {Object} index
			 * @param {Object} row
			 */
			handleEdit(index, row) {
				this.$refs.userEdit.ShowDialog(row);
			},

			handRolePreserve(index, row) {
				this.$refs.rolePreserve.showDialog(row);
			},


			getList() {
				selectByPage(this.query).then(response => {
					this.list = response.data.extend.list
					this.total = response.data.extend.total;

					let query = {};
					Object.assign(query, this.query.body);
					query.pageNum = this.query.pageNum;
					query.pageSize = this.query.pageSize;
					this.$router.push({
						query: query
					})
				}).catch(error => {
					console.info("失败");
					console.info(error.message);
				});
			},

			currentChange(val) {
				this.query.pageNum = val;
				this.getList();
			},
			sizeChange(val) {
				this.query.pageSize = val;
				this.query.pageNum = 1;
				this.getList();
			},
			fresh() {
				this.getList();
			}

		}
	}
</script>

<style>
</style>
